
package com.project2.controller;

import com.project2.anno.SaveLog;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;

import com.project2.bean.bo.CommentBo;
import com.project2.bean.po.MarketOrder;
import com.project2.service.OrderMService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.project2.bean.bo.OrderDBO;
import com.project2.bean.bo.OrderShipBO;
import com.project2.bean.po.MarketChannel;
import com.project2.bean.vo.OrderDetailVO;
import com.project2.service.OrderMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

/**
 * @Author: cc
 * @Date: 2022-08-13 17:08
 * @Description: 订单管理模块
 */
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {


    @Autowired
    OrderMService orderMService;

    /**
     *显示所有订单list
     * @param pageBo
     * @param start
     * @param end
     * @return
     */
    @RequiresPermissions("admin:order:list")
    @GetMapping("list")
    public ResultVo orderInfo(PageBo pageBo,
                              @DateTimeFormat(pattern="yyyy-MM-dd") Date start,
                              @DateTimeFormat(pattern="yyyy-MM-dd") Date end,
                              Short[] orderStatusArray,Integer userId,Long orderSn){

      CommonData date = orderMService.orderMInfo(pageBo,start,end,orderStatusArray,userId,String.valueOf(orderSn));

      return ResultVo.ok(date);
    }



    /*
    TODO 回复评论
     */
    @RequiresPermissions("admin:order:reply")
    @RequestMapping("reply")
    public ResultVo Reply(@RequestBody CommentBo commentBo){
        int i = orderMService.addContent(commentBo);
        return ResultVo.ok();
    }


    /**
     * 显示订单详情
     * @param id  订单id
     * @return
     */
    @RequiresPermissions("admin:order:detail")
    @GetMapping("detail")
    public ResultVo orderDetail(Integer id){

        OrderDetailVO orderDetail = orderMService.orderDetail(id);


        return ResultVo.ok(orderDetail);
    }


    /**
     * 删除订单
     * @param
      * @return
     */
    @RequiresPermissions("admin:order:delete")
    @SaveLog(value = "删除订单")
    @PostMapping("delete")
    public ResultVo orderDelete(@RequestBody OrderDBO bO){

    int delete = orderMService.orderDelete(bO.getOrderId());

    if (delete != 1){
        return ResultVo.wrong("删除失败");
    }

       return ResultVo.ok();
    }

    /**
     * 退款
     * @param order  包含id 和 money
     * @return
     */
    @RequiresPermissions("admin:order:refund")
    @SaveLog(value = "退款")
    @PostMapping("refund")
    public ResultVo orderRefund(@RequestBody OrderDBO order){

        int refund = orderMService.orderRefund(order);

        if (refund != 1){
            return ResultVo.wrong("退款失败");
        }
        return ResultVo.ok();
    }

    /**
     * 显示快递公司
     * @return
     */

    @GetMapping("channel")
    public ResultVo orderChannel(){

      List<MarketChannel> channels = orderMService.getChannel();

      return ResultVo.ok(channels);
    }


    /**
     * 发货
     * @return
     */
    @RequiresPermissions("admin:order:ship")
    @SaveLog(value = "发货")
    @PostMapping("ship")
    public ResultVo orderShip(@RequestBody OrderShipBO ships){


     int ship  = orderMService.orderShip(ships.getOrderId(),ships.getShipChannel(),ships.getShipSn());


     if (ship != 1){
         return ResultVo.wrong("发货失败");
     }

     return ResultVo.ok();
    }


}
